package com.example.smartagriculture.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smartagriculture.utils.Result;
import com.example.smartagriculture.service.ICityService;
import com.example.smartagriculture.entity.City;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serial;

import org.springframework.web.bind.annotation.RestController;

/**
 * CityController 前端控制器
 * @author 张芸博
 * @since 2025-03-07
 */
@RestController
@RequestMapping("/city")
public class CityController {

    @Resource
    private ICityService cityService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @Operation(description="新增或更新")
    public Result save(@RequestBody City city) {
        return Result.success(cityService.saveOrUpdate(city));
    }

    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @Operation(description="通过id删除")
    public Result delete(@PathVariable Integer id) {
        City city =cityService.getById(id);
        return Result.success(cityService.saveOrUpdate(city));
    }

    /**
    *  查询所有
    */
    @GetMapping("/all")
    @Operation(description="查询所有")
    public Result findAll() {
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<City> list=  cityService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    @Operation(description="根据id查询")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(cityService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    @Operation(description="分页查询")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(cityService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    @PostMapping("/selece")
    @Operation(description="模糊查询城市")
    public Result select(@RequestBody String Name){
        return Result.success(cityService.select(Name));
    }
    @PostMapping("/Weather")
    @Operation(description="获取目标城市的天气信息")
    public Result Weather(@RequestBody String Name) throws Exception {
        return Result.success(cityService.Weather(Name));
    }
}

